/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hisense.settings.picture;
import com.hisense.settings.util.Wrapper;

import static com.google.android.tv.settings.TvSettings.System.SCREEN_DIM_TIMEOUT;
import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
import static android.provider.Settings.System.SOUND_EFFECTS_ENABLED;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.SeekBarPreference;
import android.provider.Settings;
import android.preference.PreferenceScreen;
import android.app.ProgressDialog;
import com.android.settings.R;
import android.widget.Toast;

import java.util.List;
import android.util.Log;

/**
 * Handles settings menu for image settings.  
 *
 * @author
 */
public class ScreenSettings extends PreferenceActivity implements
        Preference.OnPreferenceChangeListener{
   
	private static final String TAG = "ScreenSettings";

	private static final String KEY_SCREEN_HPOSITION = "screen_hposition";
	private static final String KEY_SCREEN_VPOSITION = "screen_vposition";
	private static final String KEY_SCREEN_CLOCK = "screen_clock";
	private static final String KEY_SCREEN_PHASE = "screen_phase";
	private static final String KEY_SCREEN_AUTOADJUST = "screen_autoadjust";

	private static final int ASPECTRATIO_ONEBYONE = 7;
//	private SeekBarPreference mHpositionPreferrence;
	private Preference mHpositionPreferrence;
	private Preference mVpositionPreferrence;
	private Preference mClockPreferrence;
	private Preference mPhasePreferrence;
	private PreferenceScreen mVGAAutoAdjust;

	private ProgressDialog pd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.screen_settings);

		//Hposition
		mHpositionPreferrence = (Preference)findPreference(KEY_SCREEN_HPOSITION);
		//mHpositionPreferrence.setTitle(getString(R.string.screen_hposition).toString() +" : " + Wrapper.getWrapperInstance().getHorizontalPosition());
		mHpositionPreferrence.setOnPreferenceChangeListener(this);
		//Vposition
		mVpositionPreferrence = (Preference)findPreference(KEY_SCREEN_VPOSITION);
		//mVpositionPreferrence.setTitle(getString(R.string.screen_vposition).toString() +" : " + Wrapper.getWrapperInstance().getVerticalPosition());
		mVpositionPreferrence.setOnPreferenceChangeListener(this);	
		//Clock
		mClockPreferrence= (Preference)findPreference(KEY_SCREEN_CLOCK);
		//mClockPreferrence.setTitle(getString(R.string.screen_clock).toString() +" : " + Wrapper.getWrapperInstance().getClock());
		mClockPreferrence.setOnPreferenceChangeListener(this);		
		//Phase
		mPhasePreferrence= (Preference)findPreference(KEY_SCREEN_PHASE);
		//mPhasePreferrence.setTitle(getString(R.string.screen_phase).toString() +" : " + Wrapper.getWrapperInstance().getPhase());
		mPhasePreferrence.setOnPreferenceChangeListener(this);	

//		Log.d(TAG, "ScreenPara-->Clock1:" + Wrapper.getWrapperInstance().getClock());
//		Log.d(TAG, "ScreenPara-->Clock2:" + Wrapper.getWrapperInstance().getClock());
//		Log.d(TAG, "ScreenPara-->Clock3:" + Wrapper.getWrapperInstance().getClock());
//
//		Log.d(TAG, "ScreenPara-->Phase1:" + Wrapper.getWrapperInstance().getPhase());
//		Log.d(TAG, "ScreenPara-->Phase2:" + Wrapper.getWrapperInstance().getPhase());
//		Log.d(TAG, "ScreenPara-->Phase3:" + Wrapper.getWrapperInstance().getPhase());
		
		mVGAAutoAdjust = (PreferenceScreen) findPreference(KEY_SCREEN_AUTOADJUST);

		setSeekbarEnable();
		setSeekbarEnableFromAspectratio();
		setPreferenceTitle();
    }
	/**
     * if Aspectratio is 1:1 mode, set Seekbar disable
     */
	private void setSeekbarEnableFromAspectratio(){
		int aspectRatio = 0;
		aspectRatio = Wrapper.getWrapperInstance().getScreenMode();
		if(aspectRatio == ASPECTRATIO_ONEBYONE){
			mHpositionPreferrence.setEnabled(false);
			mVpositionPreferrence.setEnabled(false);
			mPhasePreferrence.setEnabled(false);
			mClockPreferrence.setEnabled(false);
			mVGAAutoAdjust.setEnabled(false);
		}
	}

	private void setSeekbarEnable(){
		int value = 0;
		Bundle bundle = null;
		bundle = new Bundle();
		
		bundle = Wrapper.getWrapperInstance().getPropertyRange("HorizontalPosition");
		value = Wrapper.getWrapperInstance().getHorizontalPosition();
		if((bundle.getInt("max") == 0) && (bundle.getInt("min") == 0)){
			mHpositionPreferrence.setTitle(getString(R.string.screen_hposition).toString() +" : " + 0);
			mHpositionPreferrence.setEnabled(false);
		}
		else if((value < bundle.getInt("min")) && (value > bundle.getInt("max"))){
			Wrapper.getWrapperInstance().setHorizontalPosition(0);
		}	
		
		bundle = Wrapper.getWrapperInstance().getPropertyRange("VerticalPosition");	
		value = Wrapper.getWrapperInstance().getVerticalPosition();
		if((bundle.getInt("max") == 0) || (bundle.getInt("min") == 0)){
			mVpositionPreferrence.setTitle(getString(R.string.screen_vposition).toString() +" : " + 0);
			mVpositionPreferrence.setEnabled(false);
		}
		else if((value < bundle.getInt("min")) || (value > bundle.getInt("max"))){
			Wrapper.getWrapperInstance().setVerticalPosition(0);
		}
				
		bundle = Wrapper.getWrapperInstance().getPropertyRange("Phase");
		value = Wrapper.getWrapperInstance().getPhase();
		if((bundle.getInt("max") == 0) && (bundle.getInt("min") == 0)){
			mPhasePreferrence.setTitle(getString(R.string.screen_phase).toString() +" : " + 0);
			mPhasePreferrence.setEnabled(false);
		}
		else if((value < bundle.getInt("min")) || (value > bundle.getInt("max"))){
			Wrapper.getWrapperInstance().setPhase(0);
			mPhasePreferrence.setTitle(getString(R.string.screen_phase).toString() +" : " + 0);
		}
		
		bundle = Wrapper.getWrapperInstance().getPropertyRange("Clock");
		value = Wrapper.getWrapperInstance().getClock();
		Log.d(TAG, "ScreenPara-->Clockvalue:" + value + 
				"max " + bundle.getInt("max") + " min " + bundle.getInt("min"));	
		if((bundle.getInt("max") == 0) && (bundle.getInt("min") == 0)){
			mClockPreferrence.setTitle(getString(R.string.screen_clock).toString() +" : " + 0);
			mClockPreferrence.setEnabled(false);		
		}
		else if((value < bundle.getInt("min")) || (value > bundle.getInt("max"))){
			Wrapper.getWrapperInstance().setClock(0);
			mClockPreferrence.setTitle(getString(R.string.screen_clock).toString() +" : " + 0);
			Log.d(TAG, "ScreenPara-->ClockDefaultSet1:" + value);
			Wrapper.getWrapperInstance().setClock(0);
			Log.d(TAG, "ScreenPara-->ClockDefaultSet2:" + Wrapper.getWrapperInstance().getClock());
			Wrapper.getWrapperInstance().setClock(0);
			Log.d(TAG, "ScreenPara-->ClockDefaultSet3:" + Wrapper.getWrapperInstance().getClock());	
		}	
	}

	private void setPreferenceTitle(){
		if(mHpositionPreferrence.isEnabled())
			mHpositionPreferrence.setTitle(getString(R.string.screen_hposition).toString() +" : " 
				+ Wrapper.getWrapperInstance().getHorizontalPosition());
		if(mVpositionPreferrence.isEnabled())
			mVpositionPreferrence.setTitle(getString(R.string.screen_vposition).toString() +" : "
				+ Wrapper.getWrapperInstance().getVerticalPosition());
		if(mClockPreferrence.isEnabled())
			mClockPreferrence.setTitle(getString(R.string.screen_clock).toString() +" : " 
				+ Wrapper.getWrapperInstance().getClock());
		else
			mClockPreferrence.setTitle(getString(R.string.screen_clock).toString() +" : " + 0);
		if(mPhasePreferrence.isEnabled())
			mPhasePreferrence.setTitle(getString(R.string.screen_phase).toString() +" : " 
				+ Wrapper.getWrapperInstance().getPhase());
		else
			mPhasePreferrence.setTitle(getString(R.string.screen_phase).toString() +" : " + 0);
	}
	private void setVgaAutoAdjust(int value) {
        Wrapper.getWrapperInstance().setAutoAdjust(value);
    }

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        if (preference == mVGAAutoAdjust) {
            try {
					Toast.makeText(this, R.string.auto_config,
                        Toast.LENGTH_SHORT).show();
					pd= ProgressDialog.show(ScreenSettings.this,"","The screen is adjusting");
					setVgaAutoAdjust(1);
					setPreferenceTitle();
					pd.dismiss();
            } catch (NumberFormatException e) {
                Log.e(TAG, " ", e);
            }
        }

		return false;
    }

    public boolean onPreferenceChange(Preference preference, Object objValue) {
        final String key = preference.getKey();
        if (KEY_SCREEN_HPOSITION.equals(key)) {
            int value = (Integer) objValue;
            try {
					Log.d(TAG, "KEY_SCREEN_HPOSITION =================== " + "KEY_SCREEN_HPOSITION");
					mHpositionPreferrence.setTitle(getString(R.string.screen_hposition).toString() +" : " + value);
            } catch (NumberFormatException e) {
                Log.e(TAG, " ", e);
            }
        }
        else if (KEY_SCREEN_VPOSITION.equals(key)) {


            int value = (Integer) objValue;
            try {
					Log.d(TAG, "KEY_SCREEN_VPOSITION =================== " + "KEY_SCREEN_VPOSITION");
					mVpositionPreferrence.setTitle(getString(R.string.screen_vposition).toString() +" : " + value);
            } catch (NumberFormatException e) {
                Log.e(TAG, " ", e);
            }
        }
        else if (KEY_SCREEN_CLOCK.equals(key)) {
            int value = (Integer) objValue;
            try {
					Log.d(TAG, "KEY_SCREEN_CLOCK =================== " + "KEY_SCREEN_CLOCK");
					mClockPreferrence.setTitle(getString(R.string.screen_clock).toString() +" : " + value);
            } catch (NumberFormatException e) {
                Log.e(TAG, " ", e);
            }
        }

        else if (KEY_SCREEN_PHASE.equals(key)) {
            int value = (Integer) objValue;
            try {
					Log.d(TAG, "KEY_SCREEN_PHASE =================== " + "KEY_SCREEN_PHASE");
					mPhasePreferrence.setTitle(getString(R.string.screen_phase).toString() +" : " + value);
            } catch (NumberFormatException e) {
                Log.e(TAG, " ", e);
            }
        }


        return true;
    }

}

